﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace job
{
    public partial class admin2 : Form
    {
        public admin2()
        {
            InitializeComponent();
            Table();
            

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
           
                string id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();//将值转换为字符串格式，获取学号,可将id换成sno，代表根据学号删除
                label2.Text= id + dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
                DialogResult dr = MessageBox.Show("确认删除吗？","信息提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Question);
                if (dr == DialogResult.OK)
                {
                    string sql = $"delete from student_news where sno ='{id}'";
                    Dao dao = new Dao();
                    if (dao.Execute(sql) > 0)
                    {
                        MessageBox.Show("删除成功");
                        Table();
                    }
                    else
                    {
                        MessageBox.Show("删除失败" + sql);
                    }
                    dao.DaoClose();


                }
            }
            catch
            {

                MessageBox.Show("请先在表格中选中要删除的学生！" ,"信息提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Error);
            }
        }

        private void admin2_Load(object sender, EventArgs e)
        {
            
        }
        public void Table()
        {
            dataGridView1.Rows.Clear();//清空旧数据
            Dao dao = new Dao();
            string sql = $"select * from student_news"; 
            IDataReader dc = dao.read(sql); //实例化dc 然后读取一个sql的数据，返回到dc
            while (dc.Read()) //一行一行进行读取
            {
                dataGridView1.Rows.Add(dc[0].ToString(),dc[1].ToString(), dc[2].ToString(), dc[3].ToString(), dc[4].ToString());
            }
            dc.Close();
            dao.DaoClose();


        }
        //学号查询
       
        public void TableID()
        {
            dataGridView1.Rows.Clear();//清空旧数据
            Dao dao = new Dao();
            string sql = string.Format("select * from company_new ");
            IDataReader dc = dao.read(sql); //实例化dc 然后读取一个sql的数据，返回到dc
            while (dc.Read()) //一行一行进行读取
            {
                dataGridView1.Rows.Add(dc[0].ToString(), dc[1].ToString(), dc[2].ToString(), dc[3].ToString());
            }
            dc.Close();
            dao.DaoClose();


        }
        //姓名查询
        
        

        private void button1_Click(object sender, EventArgs e)
        {
            admin2_add admin = new admin2_add();
            admin.ShowDialog();
           
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Table();
        }

        private void dataGridView1_Click(object sender, EventArgs e)
        {
            label2.Text = dataGridView1.SelectedRows[0].Cells[0].Value.ToString()+dataGridView1.SelectedRows[0].Cells[1].Value.ToString();//单击事件获取学号和姓名
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            { //获取值
                string sno = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
                string name = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();//传递时顺序是从0开始的，即name的下标为1，要把cells改为1，依次往下传递
                string Smajor = dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
                string Snumber= dataGridView1.SelectedRows[0].Cells[3].Value.ToString();
                string Sgoto = dataGridView1.SelectedRows[0].Cells[4].Value.ToString();

                admin2_xiugai admin = new admin2_xiugai(sno, name,Smajor,Snumber,Sgoto);
                admin.ShowDialog();
                Table();//刷新数据
            }
            catch
            {
                MessageBox.Show("ERROR");
            }
            
        }

        private void button5_Click(object sender, EventArgs e)
        {
            admin2_qiye admin = new admin2_qiye();
            admin.ShowDialog();

        }

        private void button6_Click(object sender, EventArgs e)
        {
            dataGridView1.Rows.Clear();//清空旧数据
            Dao dao = new Dao();
            string sql = $"select * from student_news where name like '%{textBox1.Text}%'  ";//模糊查询
            IDataReader dc = dao.read(sql); //实例化dc 然后读取一个sql的数据，返回到dc
            while (dc.Read()) //一行一行进行读取
            {
               dataGridView1.Rows.Add(dc[0].ToString(), dc[1].ToString(), dc[2].ToString(), dc[3].ToString(), dc[4].ToString());
            }
            textBox1.Text = "";
            dc.Close();
          dao.DaoClose();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            int n = dataGridView1.SelectedRows.Count;//获取当前选中的行数
            string sql = $"delete from student_news where sno in(";
            for(int i = 0; i < n; i++)
            {
                sql += $"'{dataGridView1.SelectedRows[i].Cells[0].Value.ToString()}',";
                
            }
            sql = sql.Remove(sql.Length - 1);//删除最后一个字符
            sql += ")";
            MessageBox.Show(sql);
            Dao dao = new Dao();
            if (dao.Execute(sql) > 0)
            { 
                MessageBox.Show($"成功删除{n}条信息");
                Table();
            }


        }

       

        private void textBox2n_TextChanged(object sender, EventArgs e)
        {
            
        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void button8_Click(object sender, EventArgs e)
        {
            try
            {
                dataGridView1.Rows.Clear();//清空旧数据
                Dao dao = new Dao();
                string sql = $"select * from student_news where sno= {textBox2.Text}  ";//模糊查询
                IDataReader dc = dao.read(sql); //实例化dc 然后读取一个sql的数据，返回到dc
                while (dc.Read()) //一行一行进行读取
                {
                    dataGridView1.Rows.Add(dc[0].ToString(), dc[1].ToString(), dc[2].ToString(), dc[3].ToString(), dc[4].ToString());
                }
                textBox2.Text = "";
                dc.Close();
                dao.DaoClose();
            }
            catch
            {
                MessageBox.Show("请输入正确学号");
            }
           
        }
    }
}
